﻿namespace EFMLEditor.Pages
{
    using System.ComponentModel.Design;
    using System.ComponentModel.Design.Serialization;
    using System.Drawing.Design;
    using System.Windows.Forms;

    using Creek.UI.EFML.Base;

    using EFMLEditor.Internal;

    using Hosting;

    public partial class DesignerPage : UserControl
    {
        #region Fields

        private ServiceContainer serviceContainer;

        #endregion

        #region Constructors and Destructors

        public DesignerPage()
        {
            this.InitializeComponent();
            this.Initialize();
        }

        #endregion

        #region Methods

        private void Initialize()
        {
            IDesignerHost host;
            Window form;
            IRootDesigner rootDesigner;
            Control view;

            // Initialise service container and designer host
            this.serviceContainer = new ServiceContainer();
            this.serviceContainer.AddService(typeof(INameCreationService), new NameCreationService());
            //   this.serviceContainer.AddService(typeof(IUIService), new UIService(this));
            host = new DesignerHost(this.serviceContainer);

            // Add toolbox service
            this.serviceContainer.AddService(typeof(IToolboxService), this.lstToolbox);
            this.lstToolbox.designPanel = this.pnlViewHost;
            this.PopulateToolbox(this.lstToolbox);

            // Add menu command service
            // this.menuService = new MenuCommandService();
            // this.serviceContainer.AddService(typeof(IMenuCommandService), this.menuService);

            // Start the designer host off with a Form to design
            form = (Window)host.CreateComponent(typeof(Window));
            form.TopLevel = false;
            form.Text = "Form1";

            // Get the root designer for the form and add its design view to this form
            rootDesigner = (IRootDesigner)host.GetDesigner(form);
            view = (Control)rootDesigner.GetView(ViewTechnology.Default);
            view.Dock = DockStyle.Fill;
            this.pnlViewHost.Controls.Add(view);

            // Subscribe to the selectionchanged event and activate the designer
            var s = (ISelectionService)this.serviceContainer.GetService(typeof(ISelectionService));
            s.SelectionChanged += this.OnSelectionChanged;
            host.Activate();
        }

        private IUIElement selection;

        private void OnSelectionChanged(object sender, System.EventArgs e)
        {
            var s = (ISelectionService)this.serviceContainer.GetService(typeof(ISelectionService));

            this.selection = s.PrimarySelection as IUIElement;
            if (this.selection != null)
            {
                if (s.SelectionCount == 0)
                {
                    this.propertyGrid.SelectedObject = null;
                }
                else
                {
                    if (this.selection != null)
                    {
                        this.propertyGrid.ShowCustomProperties = true;

                        if (this.selection.ID == null)
                        {
                            this.selection.ID = this.selection.GetType().Name;
                        }

                        this.propertyGrid.Item.Clear();
                        this.propertyGrid.Item.Add("Id", this.selection.ID, false, "General", "", true);
                        this.propertyGrid.Item.Add("Style", this.selection.style, false, "General", "", true);
                    }
                }
            }
            else
            {
                this.propertyGrid.SelectedObject = (s.PrimarySelection as Window);
            }
        }

        private void PopulateToolbox(IToolboxService toolbox)
        {
            toolbox.AddToolboxItem(new ToolboxItem(typeof(Creek.UI.EFML.Base.Controls.Link)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(Creek.UI.EFML.Base.Controls.Dropdown)));
            toolbox.AddToolboxItem(new ToolboxItem(typeof(Creek.UI.EFML.Base.Controls.Navigator.PageNavigator)));
        }

        #endregion
    }
}